﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;


namespace 凌依.工具库.U3D空间计算
{
    /// <summary>
    /// 鼠标,手指坐标返回
    /// </summary>
    public class 触控
    {
        /// <summary>
        /// 返回当前鼠标世界位置
        /// </summary>
        /// <param name="当前位置">可以理解为摄像机位置,或者点击到的物体位置</param>
        /// <returns></returns>
        public static Vector3 获取_鼠标世界坐标(Transform 当前位置)
        {
            return 获取_鼠标世界坐标(当前位置.position);
        }
        public static Vector3 获取_鼠标世界坐标(Vector3 当前位置)
        {
            Vector3 screenPos = Camera.main.WorldToScreenPoint(当前位置);
            Vector3 mousePosOnScreen = Input.mousePosition;
            mousePosOnScreen.z = screenPos.z;
            return Camera.main.ScreenToWorldPoint(mousePosOnScreen);
        }
        /// <summary>
        /// 仅限2d模式
        /// </summary>
        /// <param name="当前位置"></param>
        /// <returns></returns>
        public static Vector3 获取_鼠标世界坐标()
        {
            return Camera.main.ScreenToWorldPoint(Input.mousePosition);
        }
        public static void 射线(Transform 自身, float 射线长度)
        {
            Ray 射线 = Camera.main.ScreenPointToRay(Input.mousePosition);
            RaycastHit 点击信息;
            if (Physics.Raycast(射线, out 点击信息, 射线长度, LayerMask.GetMask("地面")))//地面图层
            {
                GameObject 碰撞对象 = 点击信息.collider.gameObject;
                Vector3 目标位置 = 点击信息.point;

                Debug.DrawLine(自身.position, 目标位置, Color.green);
            }
            else
                Debug.DrawLine(自身.position, 射线.GetPoint(射线长度), Color.green);

        }
        public static Vector3 获取_手指世界坐标(Transform 当前位置, int 手指序号)
        {
            Vector3 screenPos = Camera.main.WorldToScreenPoint(当前位置.position);
            Vector3 mousePosOnScreen = Input.touches[手指序号].position;
            mousePosOnScreen.z = screenPos.z;
            return Camera.main.ScreenToWorldPoint(mousePosOnScreen);
        }

        public static Vector3 获取_鼠标位置()
        {
            return Input.mousePosition;
        }
        public static Vector2 获取_手指位置(int 序号)
        {
            return Input.touches[序号].position;
        }
        public static int 获取_屏幕手指数量()
        {
            return Input.touchCount;
        }

        public static Vector2 获取_屏幕长宽()
        {
            return new Vector2(Screen.width, Screen.height);
        }
        /// <summary>
        /// 0左键,1右键,2中建
        /// </summary>
        /// <param name="编号"></param>
        /// <returns>检测到对应事件返回true</returns>
        public static bool 获取_鼠标点击事件(int 编号)
        {
            return Input.GetMouseButtonDown(编号);
        }

        /// <summary>
        /// 获取画布下鼠标坐标
        /// </summary>
        /// <param name="画布"></param>
        /// <returns></returns>
        public static Vector2 获取局部鼠标坐标(Canvas 画布)
        {
            Vector2 坐标;
            RectTransformUtility.ScreenPointToLocalPointInRectangle(画布.transform as RectTransform, Input.mousePosition, null, out 坐标);
            return 坐标;
        }
        public static Vector2 获取局部鼠标坐标(Transform 画布)
        {
            Vector2 坐标;
            RectTransformUtility.ScreenPointToLocalPointInRectangle(画布 as RectTransform, Input.mousePosition, null, out 坐标);
            return 坐标;
        }
        public static Vector2 获取局部鼠标坐标(RectTransform 画布)
        {
            Vector2 坐标;
            RectTransformUtility.ScreenPointToLocalPointInRectangle(画布, Input.mousePosition, null, out 坐标);
            return 坐标;
        }
    }
}